package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.MyResponseResult;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 部门管理Controller
 */
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("/depts")
    public MyResponseResult<List<Dept>> selectAll(){
        List<Dept> depts = deptService.selectAll();
        return MyResponseResult.ok(depts);
    }

    @DeleteMapping("/depts/{id}")
    public MyResponseResult<Object> delById(@PathVariable Integer id){
        //参调响
        int i = deptService.delById(id);
        return i>=1?MyResponseResult.ok("ok"):MyResponseResult.error(id+"为部门删除失败，请确定id后重试");
    }
    @PostMapping("/depts")
    public MyResponseResult<Object> saveDpet(@RequestBody Dept dept){
        int i = deptService.insertDept(dept);
        return i>=1?MyResponseResult.ok("ok"):MyResponseResult.error("部门"+dept.getName()+"添加失败");
    }



}
